<?php
/**
 * same as Utils but is focused on theme modifications
 */

    if (! class_exists('NaturalExtension_DefaultActions')) {
        class NaturalExtension_DefaultActions {
            private $utils;

            public function __construct() {
                $this->utils = new NaturalExtension_Utils();
            }

            /**
             * Creates a Wordpress Navigation Menu
             * @param  [string] $slug the menu slug
             * @param  [string] $name the menu name
             * @return [int] returns the menu id
             */
            public function create_menu($slug, $name) {
                $menu_locations = get_theme_mod('nav_menu_locations');

                if (! wp_get_nav_menu_object($name)) {
                    $menu_id = wp_create_nav_menu($name);

                    if (! has_nav_menu($slug)) {
                        $menu_locations[$slug] = $menu_id;
                        set_theme_mod('nav_menu_locations', $menu_locations);
                    }
                }

                return isset($menu_id) ? $menu_id : $menu_locations[$slug];
            }

            /**
             * creates a Wordpress link category
             * @param  [string] $name the name of the category
             * @return [void]
             */
            public function create_link_category($name) {
                $categories = get_terms('link_category', array('name__like' => $name, 'hide_empty' => false));

                if (sizeof($categories) == 0) {
                    wp_insert_category(array(
                        'taxonomy'  => 'link_category',
                        'cat_name'  => $name
                    ));
                }
            }

            /**
             * sets the default options and theme behavior
             * @param [string] $service_type the sevice level
             */
            public function set_theme_defaults($service_type) {
                // Delete or Update some pages/posts
                // ------------------------------

                // delete Sample page and Hello World Post
                // as of WP 3.4.2
                // Hello Wolrd post id is 1
                // Sample Page post id is 2
                wp_delete_post(1);  // deletes the sample post
                wp_delete_post(2);  // deletes the sample page


                if ($service_type == 'auwebsite') {
                    // Add a default blog post for author websites
                    wp_insert_post(array(
                        'post_content'  => 'Blog content here',
                        'post_title'    => 'Welcome to my new blog!',
                        'post_status'   => 'publish'
                    ));
                }


                // Update "Author" role capabilities
                // ------------------------------

                $author_role        = get_role('author');
                $author_caps_addon  = array(
                    'edit_others_posts',
                    'edit_theme_options',
                    'moderate_comments',
                    'activate_plugins',
                    'update_plugins',
                    'install_plugins',
                    'manage_categories'
                );

                foreach ($author_caps_addon as $capability) {
                    $author_role->add_cap($capability);
                }


                // Update Reading Settings
                // ------------------------------

                // update to a static page
                $this->utils->_wp_update_option('show_on_front', 'page');

                // Front page should be home
                if (! is_null($this->utils->get_page_id_via_name('home'))) {
                    $home_page_id = $this->utils->get_page_id_via_name('home');
                    $this->utils->_wp_update_option('page_on_front', $home_page_id);
                }

                // Posts page should be blog
                if (! is_null($this->utils->get_page_id_via_name('blog'))) {
                    $blog_page_id = $this->utils->get_page_id_via_name('blog');
                    $this->utils->_wp_update_option('page_for_posts', $blog_page_id);
                }


                // Update some options
                // ------------------------------

                // updating this option to 1 ensures that a designer
                // doesn't get back to the page builder page
                $this->utils->set_option('service_chosen', '1');

                // update permalink structure to postname only if it is not
                // cuts the process of going to Settings -> Permalinks
                // and manually updating
                $this->utils->_wp_update_option('permalink_structure', '/%postname%/');


                // Set defaults for Theme Option
                // ------------------------------

                // set default fav icon
                $this->utils->_wp_update_option('e404_custom_favicon', OF_DIRECTORY . '/patch/images/icons/favicon.png');

                // set 'Theme Layout' to boxed
                $this->utils->_wp_update_option('e404_theme_layout', 'boxed');

                // enable breadcrumb trail
                $this->utils->_wp_update_option('e404_breadcrumbs', 'true');

                // show page titles
                $this->utils->_wp_update_option('e404_page_titles', 'true');

                // check 'Enable PrettyPhoto'
                $this->utils->_wp_update_option('e404_gallery_prettyphoto', 'true');

                // uncheck 'Allow Page Comments'
                $this->utils->_wp_update_option('e404_page_comments', 'false');

                // update top contact box text
                $this->utils->_wp_update_option('e404_top_contact_box', 'Contact the Author | sample_mail@mail.com');


                // Styling Options
                // ------------------------------

                // add custom styles
                $this->utils->_wp_update_option('e404_custom_css', file_get_contents(OF_FILEPATH . '/patch/css/custom-styles.css'));


                // Intro Text boxes
                // --------------------

                // set disable 'Intro Textbox'
                $this->utils->_wp_update_option('e404_intro_type', 'none');

                // make sure home page intro text type is set to 'html'
                $this->utils->_wp_update_option('e404_home_intro_type', 'html');

                // set default text for home page 'Intro Textbox'
                $this->utils->_wp_update_option('e404_home_intro_text', '<h1 class="center">Book Title Here</h1><h2 class="center">Author&rsquo;s Name Here</h2>');

                // disable 'Intro Text Box for Blog Pages'
                $this->utils->_wp_update_option('e404_blog_intro_type', 'none');

                // disable 'Intro Text Box for Portfolio Pages'
                $this->utils->_wp_update_option('e404_portfolio_intro_type', 'none');

                // disable 'Intro Text Box for Gallery Pages'
                $this->utils->_wp_update_option('e404_gallery_intro_type', 'none');

                // set footer columns to 3
                $this->utils->_wp_update_option('e404_footer_columns', '3');

                // update footer texts
                $this->utils->_wp_update_option('e404_footer_below_left', 'Copyright &copy; 2012. <a href="' . home_url() . '">Authorname</a>. All rights reserved.');
                $this->utils->_wp_update_option('e404_footer_below_right', '&nbsp;');
            }

            public function show_admin_messages() {
                // if admin, show messages when
                if (current_user_can('add_users')) {
                    // the required plugins are not installed
                    $this->required_plugins_messages();

                    // this is migrated
                    $this->migration_messages();
                }
            }

            private function required_plugins_messages() {
                $plugins = $this->utils->config['required_plugins'];
                $anti_spam_msg = 'In order to block/reduce spam comments from your blog. It is highly recommended to install and activate the plugin(s) listed below.';
                $num_of_anti_spam_plugin_installed = 0;

                foreach ($plugins['anti_spam'] as $anti_spam) {
                    if (! is_plugin_active($anti_spam['plugin_dir'])) {
                        $please_activate_msg = 'Please activate this plugin';
                        $anti_spam_msg .= '<br><strong>' . $anti_spam['name'] . '</strong>';
                    } else {
                        $num_of_anti_spam_plugin_installed += 1;
                    }
                }

                if ($num_of_anti_spam_plugin_installed < sizeof($plugins['anti_spam'])) {
                    $this->utils->show_message('Don&rsquo;t forget to install anti-spam plugin(s)', $anti_spam_msg, true);
                }
            }

            private function migration_messages() {
                $domain = $_SERVER['HTTP_HOST'];

                // show message outside of goindiebooks & xlibrishub only
                // sometimes, we setup a website even if it's not available
                // on bookstore yet, so only show the notice once this gets live
                if (! preg_match('/(goindiebooks|xlibrishub)/i', $domain)) {
                    $books = $this->utils->get_bookshelf_items();

                    if (sizeof($books) > 0) {
                        $landing_errors = array();
                        $gemini_valid_link_pattern = '/(bookstore\.)?[^\.]*\.com\/Products\/SKU-\d*/';
                        $xl_valid_link_pattern = '/bookid=\d{1,7}$/';
                        $xl_link_pattern = '/xlibris(publishing)?\.com?/';

                        foreach ($books as $book) {
                            $bookstore_link = get_post_meta($book->ID, 'bookstore_link', true);

                            // check for imprint variation first
                            if (preg_match($xl_link_pattern, $bookstore_link)) {
                                // this is a xlibris imprint
                                if (! preg_match($xl_valid_link_pattern, $bookstore_link)) {
                                    $landing_errors[] = $book->post_title;
                                }
                            } else {
                                // this is a gemini imprint
                                if (! preg_match($gemini_valid_link_pattern, $bookstore_link)) {
                                     $landing_errors[] = $book->post_title;
                                }
                            }
                        }

                        if (sizeof($landing_errors) > 0) {
                            $bookstore_link_msg = 'The following book(s) might have an incorrect landing page. Please check and update.';
                            $bookstore_link_msg .= '<br>- ';
                            $bookstore_link_msg .= implode('<br>- ', $landing_errors);

                            $this->utils->show_message('Bookstore link', $bookstore_link_msg, true);
                        }
                    }
                }
            }
        }
    }
?>